//
//  FirstViewController.m
//  SEIO2012
//
//  Created by Gonzalo Abella Dago on 19/05/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FirstViewController.h"


@implementation FirstViewController

@synthesize mainView;

@synthesize logo;
@synthesize myTitle;
@synthesize mySubtitle;
@synthesize myView;

@synthesize presentationView;
@synthesize cover;
@synthesize presentation;

@synthesize homeText;

- (void)dealloc {
	[mainView release];
	[logo release];
	[myTitle release];
	[mySubtitle release];
	[myView release];
	[presentationView release];
	[cover release];
	[presentation release];
	[homeText release];
    [super dealloc];
}


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	NSLog(@"FirstViewController viewDidLoad");
	
	// Get data from Web Service
	ConferenceInfoDTO *myConferenceInfo = [[ConferenceInfoDTO alloc] init];
	ConferenceInfoWS *conferenceInfoWS = [[ConferenceInfoWS alloc] initWithConferenceInfoItem:myConferenceInfo];
	[conferenceInfoWS setPrincipalURL:@"http://www.seio2012.com/es/api/conference_info.xml"];
	[conferenceInfoWS setLastModifiedURL:@"http://www.seio2012.com/es/api/conference_info.xml?last_modified=1"];
	[conferenceInfoWS start];
	
	// Init title and subtitle
	[self.myTitle setText:[myConferenceInfo title]];
	[self.mySubtitle setText:[myConferenceInfo title2]];
	[self.presentation setText:[myConferenceInfo presentation]];
	
	// Init logo
	NSURL *logoURL = [[NSURL alloc] initWithString:[myConferenceInfo logo_frontend]];
	NSData *logoData = [[NSData alloc] initWithContentsOfURL:logoURL];
	UIImage *logoImage = [[UIImage alloc] initWithData:logoData];
	[logo setImage:logoImage];
	NSLog(@"URL del logo: %@", [myConferenceInfo logo_frontend]);
	[logoURL release];
	[logoData release];
	[logoImage release];
	
	// Init cover image
	NSArray *coverArray = [myConferenceInfo cover_image];
	NSURL *coverURL = [[NSURL alloc] initWithString:[coverArray lastObject]];
	NSData *coverData = [[NSData alloc] initWithContentsOfURL:coverURL];
	UIImage *coverImage = [[UIImage alloc] initWithData:coverData];
	[cover setImage:coverImage];
	[coverURL release];
	[coverData release];
	[coverImage release];
	
	// Init home text
	[homeText loadHTMLString:[myConferenceInfo home_text] baseURL:[NSURL URLWithString:@""]];
	
	// Set mainView size
	[mainView setContentSize:CGSizeMake(320.0, 1000.0)];
	[mainView setShowsVerticalScrollIndicator:YES];

	// Set size and position of titleView
	CGRect titleViewFrame = CGRectMake(10.0, 10.0, 300.0, 80.0);
	self.myView.frame = titleViewFrame;
	
	// Size and position of presentation label
	CGSize suggestedPresentationLabelSize = [self.presentation.text sizeWithFont:[self.presentation font]
															   constrainedToSize:CGSizeMake(100.0, FLT_MAX)
																   lineBreakMode:UILineBreakModeWordWrap];
	CGRect presentationFrame = self.presentation.frame;
	presentationFrame.size = suggestedPresentationLabelSize;
	self.presentation.frame = presentationFrame;
	
	[conferenceInfoWS release];
	[myConferenceInfo release];
	
    [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


@end
